<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>
          <el-button type="danger" @click="handleDeleteAll">批量删除</el-button>

          <el-form :inline="true" :model="listQuery" ref="listQuery" class="demo-form-inline">
            <el-form-item label="">
              <el-select filterable v-model="listQuery.group_type" placeholder="配置分组">
                <el-option
                  v-for="item in groupOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input type="text" v-model="listQuery.keyword" placeholder="配置名称/配置标识"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table v-loading="listLoading" ref="dataList" :data="menuList" @selection-change="handleSelectionChange"
                  style="width:100%;margin-bottom: 20px;" row-key="id" border
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column type="selection"></el-table-column>
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="name" label="配置名称"></el-table-column>
          <el-table-column prop="key" label="配置标识"></el-table-column>
          <el-table-column prop="group" label="分组">
            <template slot-scope="scope">
              <p v-if="scope.row.group === 1">基本设置</p>
              <p v-if="scope.row.group === 15">短信接口</p>
              <p v-if="scope.row.group === 30">APP消息推送</p>
              <p v-if="scope.row.group === 40">系统参数</p>
              <p v-if="scope.row.group === 60">程序员设置</p>
              <p v-if="scope.row.group === 65">返利设置</p>
              <p v-if="scope.row.group === 100">项目设置</p>
            </template>
          </el-table-column>
          <el-table-column prop="value" label="配置值"></el-table-column>
          <el-table-column prop="sort" label="排序"></el-table-column>
          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)" plain>编辑
              </el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)" plain>删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px">
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
                <el-button @click="deleteDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmDelete">确 定</el-button>
            </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="dynamicValidateForm" :rules="dynamicValidateRules" ref="dynamicValidateForm" label-width="120px">
        <el-form-item label="配置标识" prop="key">
          <el-input v-model="dynamicValidateForm.key" placeholder="请输入配置标识" clearable></el-input>
        </el-form-item>
        <el-form-item label="配置标题" prop="name">
          <el-input v-model="dynamicValidateForm.name" placeholder="请输入配置标题" clearable></el-input>
        </el-form-item>
        <el-form-item label="配置分组" prop="group">
          <el-select filterable v-model="dynamicValidateForm.group" clearable placeholder="选择参数所属配置分组">
            <el-option
              v-for="item in groupOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置类型" prop="type">
          <el-select filterable v-model="dynamicValidateForm.type" clearable placeholder="选择参数所属配置类型">
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置值" prop="value">
          <el-input type="textarea" v-model="dynamicValidateForm.value" placeholder="请输入配置值"></el-input>
        </el-form-item>
        <el-form-item label="配置项说明" prop="desc">
          <el-input type="textarea" v-model="dynamicValidateForm.desc" placeholder="请输入配置项说明"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="dynamicValidateForm.sort" clearable placeholder="从小到大排序"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-radio v-model="dynamicValidateForm.status" :label='1'>禁用</el-radio>
          <el-radio v-model="dynamicValidateForm.status" :label='0'>启用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmSubmit('dynamicValidateForm')">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getConfigTypeList,
  listData,
  postCreate,
  putEdit,
  deleteList
} from '@/api/config/listConfig'

export default {
  data() {
    return {
      // 要修改的当前行的id
      row_id: 0,
      dynamicValidateRules: {
        key: [
          {required: true, message: '请填写标题文字', trigger: 'blur'},
          {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请填写标题文字', trigger: 'blur'},
          {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
        ],
        group: [
          {required: true, message: '请选择配置分组', trigger: 'blur'},
        ],
        type: [
          {required: true, message: '请选择配置', trigger: 'blur'},
        ],
        value: [
          {required: true, trigger: 'blur'},
        ],
        sort: [
          {type: 'number', required: true, message: '请输入排序', trigger: 'blur', min: 0, max: 100},
        ],
      },
      dynamicValidateForm: {
        key: undefined,
        name: undefined,
        group: undefined,
        type: undefined,
        value: undefined,
        desc: undefined,
        sort: undefined,
        status: 1,
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      listLoading: false,
      total: 0,
      menuList: [],
      listQuery: {
        page: 1,
        perpage: 10,
        group_type: undefined,
        keyword: undefined,
      },
      typeOptions: [],
      groupOptions: [],
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: '',

    }
  },
  created() {
    this.getGroupList()
    this.getList()
  },
  methods: {
    // 获取参数配置类型
    getGroupList() {
      getConfigTypeList().then(response => {
        for (let key in response.data.group_map) {
          this.groupOptions.push({value: parseInt(key), label: response.data.group_map[key]})
        }
        response.data.type_map.forEach((item, index) => {
          this.typeOptions.push({value: index, label: item})
        })
      })
    },
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      }
      if (this.listQuery.group_type) {
        urlData = urlData + `&group_type=${this.listQuery.group_type}`;
      }
      if (this.listQuery.keyword) {
        urlData = urlData + `&keyword=${this.listQuery.keyword}`;
      }
      // 发送异步请求
      // this.listLoading = false
      listData(urlData).then(response => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total
        this.listLoading = false
      })
    },
    // 点击查询
    searchSubmit() {
      this.listQuery.page = 1
      this.getList()
    },
    // 点击重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perpage = 10
      this.listQuery.group_type = undefined;
      this.listQuery.keyword = undefined
    },
    // 点击新增
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.row_id = row.id
      this.dynamicValidateForm.key = row.key
      this.dynamicValidateForm.name = row.name
      this.dynamicValidateForm.group = row.group
      this.dynamicValidateForm.type = row.type
      this.dynamicValidateForm.value = row.value
      this.dynamicValidateForm.desc = row.desc
      this.dynamicValidateForm.sort = row.sort
      this.dynamicValidateForm.status = row.status
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm() {
      this.dynamicValidateForm = {
        key: undefined,
        name: undefined,
        group: undefined,
        type: undefined,
        value: undefined,
        desc: undefined,
        sort: undefined,
        status: '1',
      }
    },
    confirmSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogStatus === 'create') {
            postCreate(this.dynamicValidateForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false;
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.$message({
                  message: response.message,
                  type: 'error'
                });
              }
            })
          } else {
            putEdit(this.row_id, this.dynamicValidateForm).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false;
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.$message({
                  message: response.message,
                  type: 'error'
                });
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = {ids: `${row.id}`}
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        });
        return;
      } else if (this.multipleSelection.length !== 0) {
        let ids = '';
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = {ids: ids}
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      this.deleteDialogVisible = false;
      deleteList(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false;
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          });
        } else {
          this.$message({
            message: response.message,
            type: 'error'
          });
        }
      })
    },
  }
}
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
